package api

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type BaseController struct{}

func (con BaseController) successMessage(c *gin.Context, message string, data interface{}) {
	if data == nil {
		data = gin.H{}
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  message,
		"code": 200,
		"data": data,
	})
}

func (con BaseController) errorMessage(c *gin.Context, message string, errorCode int) {
	c.JSON(http.StatusOK, gin.H{
		"msg":  message,
		"code": errorCode,
	})
}

func (con BaseController) pageSuccess(c *gin.Context, List interface{}, total int64) {
	pageInfo, _ := c.Get("pageRequest")
	pageData := pageInfo.(gin.H)
	c.JSON(http.StatusOK, gin.H{
		"data": gin.H{
			"list":     List,
			"page":     pageData["page"],
			"per_page": pageData["pageSize"],
			"total":    total,
		},
		"code": 200,
		"msg":  "查询成功",
	})
}
